package com.wjr;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Author: wjr
 * @Description: jedis连接池访问redis
 * @Date Created in  2020-10-04 4:10
 * @Modified By:
 */
public class Demo1 {
    public static void main(String[] args) {
        //1.创建连接池配置工具类对象
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(10);//设置jedis连接的空闲数
        jedisPoolConfig.setMaxTotal(20);//设置总连接数
        JedisPool jedisPool = null;
        Jedis jedis = null;

        try {
            //2.创建连接池对象
            jedisPool = new JedisPool(jedisPoolConfig, "192.168.168.101", 6379);

            //3.获取jedis资源
            jedis = jedisPool.getResource();

            //4.操作jedis数据
            jedis.set("user2","wjr");
            String s = jedis.get("user2");
            System.out.println(s);
        } finally {
            //5.关闭资源
            if (jedis!=null){
                jedis.close();
            }
            if (jedisPool!=null){
                jedisPool.close();
            }
        }
    }
}
